package com.ctsi.ssdc.web;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.lang.Integer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ctsi.ssdc.service.LitemallOrderService;
import com.ctsi.ssdc.domain.LitemallOrder;

import com.ctsi.ssdc.exception.BadRequestAlertException;
import com.ctsi.ssdc.model.PageResult;
import com.ctsi.ssdc.util.HeaderUtil;
import com.ctsi.ssdc.util.ResponseUtil;


/**
 * REST controller for managing LitemallOrder.
 *
 * @author ctsi-biyi-generator
 *
 */
@Api(value = "/api",tags = {"litemall-order-controller"})
@RestController
@RequestMapping("/api")
public class LitemallOrderController {

    private final Logger log = LoggerFactory.getLogger(LitemallOrderController.class);

    private static final String ENTITY_NAME = "litemallOrder";

    private final LitemallOrderService litemallOrderService;

    public LitemallOrderController(LitemallOrderService litemallOrderService) {
        this.litemallOrderService = litemallOrderService;
    }

    @InitBinder   
    public void initBinder(WebDataBinder binder) {   
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
        dateFormat.setLenient(true);   
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
    } 
   
    /**
     * POST  /litemallOrders : Create a new litemallOrder.
     *
     * @param litemallOrder the litemallOrder to create
     * @return the ResponseEntity with status 201 (Created) and with body the new litemallOrder,
     * or with status 400 (Bad Request) if the litemallOrder has already an id
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "body",dataType = "LitemallOrder",name = "litemallOrder",value = "the litemallOrder to create")
    })
    @ApiOperation(value = "POST  /litemallOrders : create a new litemallOrder.",
        notes = "POST  /litemallOrders : create a new litemallOrder.",httpMethod = "POST")
    @PostMapping("/litemallOrders")
    public ResponseEntity<LitemallOrder> createLitemallOrder(@RequestBody LitemallOrder litemallOrder) throws URISyntaxException {
        log.debug("REST request to save LitemallOrder : {}", litemallOrder);
        if (litemallOrder.getId() != null) {
            throw new BadRequestAlertException("A new litemallOrder cannot already have an id", ENTITY_NAME, "idexists");
        }
        LitemallOrder result = litemallOrderService.insert(litemallOrder);
        return ResponseEntity.created(new URI("/api/litemallOrders" + "/" +result.getId() ))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }
	
    /**
     * PUT  /litemallOrders : Updates an existing litemallOrder.
     *
     * @param litemallOrder the litemallOrder to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated litemallOrder,
     * or with status 400 (Bad Request) if the litemallOrder is not valid,
     * or with status 500 (Internal Server Error) if the litemallOrder couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "body",dataType = "LitemallOrder",
        name = "litemallOrder",value = "the litemallOrder to update")
    })
    @ApiOperation(value = "PUT  /litemallOrders : updates an existing litemallOrder.",
        notes = "PUT  /litemallOrders : updates an existing litemallOrder.",httpMethod = "PUT")
    @PutMapping("/litemallOrders")
    public ResponseEntity<LitemallOrder> updateLitemallOrder(@RequestBody LitemallOrder litemallOrder) throws URISyntaxException {
        log.debug("REST request to update LitemallOrder : {}", litemallOrder);
	    if (litemallOrder.getId() == null) {
	    	return createLitemallOrder(litemallOrder);
	    }
        LitemallOrder result = litemallOrderService.update(litemallOrder);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * GET  /litemallOrders/:id : get the "id" litemallOrder.
     *
     * @param id the id of the litemallOrder to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the litemallOrder, or with status 404 (Not Found)
     */
//    @ApiImplicitParams({
//        @ApiImplicitParam(paramType = "path",dataType = "INTEGER",name = "id",
//        value = "the id of the litemallOrder to retrieve")
//    })
//    @ApiOperation(value = "GET  /litemallOrders/id : get the id litemallOrder.",
//        notes = "GET  /litemallOrders/id : get the id litemallOrder.",httpMethod = "GET")
//    @GetMapping("/litemallOrders/{id}")
//    public ResponseEntity<LitemallOrder> getLitemallOrder(@PathVariable Integer id) {
//        log.debug("REST request to get LitemallOrder : {}", id);
//        LitemallOrder litemallOrder = litemallOrderService.findOne(id);
//        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(litemallOrder));
//    }

    /**
    * GET  /litemallOrders : get the litemallOrders firstStringBaseColumn.
    *
    * @return the ResponseEntity with status 200 (OK) and the list of litemallOrders in body
    */
    @ApiOperation(value = "GET  /litemallOrders : get the litemallOrders firstStringBaseColumn.",
                notes = "GET  /litemallOrders : get the litemallOrders firstStringBaseColumn.",httpMethod = "GET")
    @GetMapping("/litemallOrders")
    public PageResult<LitemallOrder> getLitemallOrdersList(String orderSn ,Pageable pageable) {
        log.debug("REST request to get LitemallOrdersList");
        return litemallOrderService.findFirstStringColumn(orderSn ,pageable);
    }
	
    /**
     * DELETE  /litemallOrders/:id : delete the "id" litemallOrder.
     *
     * @param id the id of the litemallOrder to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path",dataType = "INTEGER",
            name = "id",value = "the id of the litemallOrder to delete")
    })
    @ApiOperation(value = "DELETE  /litemallOrders/id : delete the id litemallOrder.",
        notes = "DELETE  /litemallOrders/id : delete the id litemallOrder.",httpMethod = "DELETE")
    @DeleteMapping("/litemallOrders/{id}")
    public ResponseEntity<Void> deleteLitemallOrder(@PathVariable Integer id) {
        log.debug("REST request to delete LitemallOrder : {}", id);
        litemallOrderService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
    
}
